/********************************** (C) COPYRIGHT *******************************
 * File Name          : CH392CMD.C
 * Author             : WCH
 * Version            : V1.1
 * Date               : 2022/12/28
 * Description        : CH392 command interface file
 *******************************************************************************/
#include "includes.h"
#include "CH392INC.H"
#include "ch392cmd.h"
#include "debug.h"
#include "CH392UART.h"
#include "CH392SPI_HW.h"

/********************************************************************************
 * Function Name  : CH392Reset
 * Description    : Reset the CH392
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392Reset(void)
{
//    GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET);
		gpio_bit_set(GPIOA, GPIO_PIN_12);	//led2 off
    Delay_Ms(10);
//    GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET);
		gpio_bit_set(GPIOA, GPIO_PIN_12);	//led2 off
}

/********************************************************************************
 * Function Name  : CH392CMDReset
 * Description    : Reset the CH392
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDReset(void)
{
    xWriteCH392Cmd(CMD00_RESET_ALL);
    xEndCH392Cmd();
}

/********************************************************************************
 * Function Name  : CH392CMDSleep
 * Description    : Obtain the chip and firmware version number
 * Input          : None
 * Output         : None
 * Return         : Version number
 *******************************************************************************/
u8 CH392CMDGetVer(void)
{
    u8 i;
    xWriteCH392Cmd(CMD01_GET_IC_VER);
    i = xReadCH392Data();
    xEndCH392Cmd();
    return i;
}

/********************************************************************************
 * Function Name  : CH392CMDCheckExist
 * Description    : Test communication interface and working condition
 * Input          : testdata
 * Output         : None
 * Return         : bitwise inverse of input data
 *******************************************************************************/
u8 CH392CMDCheckExist(u8 testdata)
{
    u8 i;
    xWriteCH392Cmd(CMD11_CHECK_EXIST);
    xWriteCH392Data(testdata);
/*In between sending and receiving bytes, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    i = xReadCH392Data();
    xEndCH392Cmd();
    return i;
}

/*******************************************************************************
 * Function Name  : CH392CMDGetPHYStatus
 * Description    : Gets the current PHY status
 * Input          : None
 * Output         : None
 * Return         : Current PHY status. See PHY Parameter definition for the status definition
 *******************************************************************************/
u8 CH392CMDGetPHYStatus(void)
{
    u8 i;
    xWriteCH392Cmd(CMD01_GET_PHY_STATUS);
    i = xReadCH392Data();
    xEndCH392Cmd();
    return i;
}

/********************************************************************************
 * Function Name  : CH392CMDInitCH392
 * Description    : Initialize CH392
 * Input          : None
 * Output         : None
 * Return         : u8 s
 *******************************************************************************/
u8 CH392CMDInitCH392(void)
{
    u8 i = 0;
    u8 s = 0;

    xWriteCH392Cmd(CMD0W_INIT_CH392);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW; /* Timeout exits. This function needs more than 400MS to complete */
    }
    return s;
}

/********************************************************************************
 * Function Name  : CH392CMDSetUartBaudRate
 * Description    : Set the baudrate for serial port communication
 * Input          : baudrate
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetUartBaudRate(UINT32 baudrate)
{
    xWriteCH392Cmd(CMD31_SET_BAUDRATE);
    xWriteCH392Data((u8)baudrate);
    xWriteCH392Data((u8)((u16)baudrate >> 8));
    xWriteCH392Data((u8)(baudrate >> 16));
    xEndCH392Cmd();
}

/*******************************************************************************
 * Function Name  : CH392GetCmdStatus
 * Description    : Obtain the command execution status
 * Input          : None
 * Output         : None
 * Return         : Command execution status
 *******************************************************************************/
u8 CH392CMDGetCmdStatus(void)
{
    u8 i;
    xWriteCH392Cmd(CMD01_GET_CMD_STATUS);
    i = xReadCH392Data();
    xEndCH392Cmd();
    return i;
}

/********************************************************************************
 * Function Name  : CH392CMDSetIPAddr
 * Description    : Set IP address
 * Input          : ipaddr
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetIPAddr(u8 *ipaddr)
{
    u8 i;
    xWriteCH392Cmd(CMD40_SET_IP_ADDR);
    for (i = 0; i < 4; i++)
        xWriteCH392Data(*ipaddr++);
    xEndCH392Cmd();
}

/********************************************************************************
 * Function Name  : CH392CMDSetGWIPAddr
 * Description    : Set GWIP address
 * Input          : ipaddr
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetGWIPAddr(u8 *gwipaddr)
{
    u8 i;
    xWriteCH392Cmd(CMD40_SET_GWIP_ADDR);
    for (i = 0; i < 4; i++)
        xWriteCH392Data(*gwipaddr++);
    xEndCH392Cmd();
}

/********************************************************************************
 * Function Name  : CH392CMDSetMASKAddr
 * Description    : Set MASK address��The default is 255.255.255.0.
 * Input          : maskaddr
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetMASKAddr(u8 *maskaddr)
{
    u8 i;
    xWriteCH392Cmd(CMD40_SET_MASK_ADDR);
    for (i = 0; i < 4; i++)
        xWriteCH392Data(*maskaddr++);
    xEndCH392Cmd();
}

/********************************************************************************
 * Function Name  : CH392CMDSetMACAddr
 * Description    : Set MAC address
 * Input          : amcaddr
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetMACAddr(u8 *amcaddr)
{
    u8 i;
    xWriteCH392Cmd(CMD60_SET_MAC_ADDR);
    for (i = 0; i < 6; i++)
        xWriteCH392Data(*amcaddr++);
    xEndCH392Cmd();
}

/********************************************************************************
 * Function Name  : CH392CMDGetMACAddr
 * Description    : Gets the MAC address
 * Input          : MAC address pointer
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDGetMACAddr(u8 *amcaddr)
{
    u8 i;
    xWriteCH392Cmd(CMD06_GET_MAC_ADDR);
    for (i = 0; i < 6; i++)
        *amcaddr++ = xReadCH392Data();
    xEndCH392Cmd();
}

/********************************************************************************
 * Function Name  : CH392CMDGetUnreachIPPT
 * Description    : Get unreachable information (IP,Port,Protocol Type)
 * Input          : list
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDGetUnreachIPPT(u8 *list)
{
    u8 i;
    xWriteCH392Cmd(CMD08_GET_UNREACH_IPPORT);
    for (i = 0; i < 8; i++)
    {
        *list++ = xReadCH392Data();
    }
    xEndCH392Cmd();
}

/********************************************************************************
* Function Name  : CH392CMDGetRemoteIPP
* Description    : Obtain the port and IP address of the remote end
* Input          : sockindex
                   list : Save the IP address and port number
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDGetRemoteIPP(u8 sockindex, u8 *list)
{
    u8 i;

    xWriteCH392Cmd(CMD06_GET_REMOT_IPP_SN);
    xWriteCH392Data(sockindex);
/*In between sending and receiving bytes, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    for (i = 0; i < 6; i++)
    {
        *list++ = xReadCH392Data();
    }
    xEndCH392Cmd();
}

/*******************************************************************************
* Function Name  : CH392SetSocketDesIP
* Description    : Set the destination IP address of socket n
* Input          : sockindex
                   ipaddr
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDSetSocketDesIP(u8 sockindex, u8 *ipaddr)
{
    xWriteCH392Cmd(CMD50_SET_IP_ADDR_SN);
    xWriteCH392Data(sockindex);
    xWriteCH392Data(*ipaddr++);
    xWriteCH392Data(*ipaddr++);
    xWriteCH392Data(*ipaddr++);
    xWriteCH392Data(*ipaddr++);
    xEndCH392Cmd();
}

/*******************************************************************************
* Function Name  : CH392SetSocketProtType
* Description    : Set the protocol type of socket n
* Input          : sockindex
                   prot type
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDSetSocketProtType(u8 sockindex, u8 prottype)
{
    xWriteCH392Cmd(CMD20_SET_PROTO_TYPE_SN);
    xWriteCH392Data(sockindex);
    xWriteCH392Data(prottype);
    xEndCH392Cmd();
}

/*******************************************************************************

* Function Name  : CH392SetSocketDesPort
* Description    : Set the destination port of socket n
* Input          : sockindex
                   des prot
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDSetSocketDesPort(u8 sockindex, u16 desprot)
{
    xWriteCH392Cmd(CMD30_SET_DES_PORT_SN);
    xWriteCH392Data(sockindex);
    xWriteCH392Data((u8)desprot);
    xWriteCH392Data((u8)(desprot >> 8));
    xEndCH392Cmd();
}

/*******************************************************************************
* Function Name  : CH392SetSocketSourPort
* Description    : Set the source port of socket n
* Input          : sockindex
                   des prot
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDSetSocketSourPort(u8 sockindex, u16 surprot)
{
    xWriteCH392Cmd(CMD30_SET_SOUR_PORT_SN);
    xWriteCH392Data(sockindex);
    xWriteCH392Data((u8)surprot);
    xWriteCH392Data((u8)(surprot >> 8));
    xEndCH392Cmd();
}

/********************************************************************************
* Function Name  : CH392SendData
* Description    : Writes data to socket n buffer
* Input          : sockindex
                   data buf
                   len
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDSendData(u8 sockindex, u8 *databuf, u16 len)
{
    u16 i;

    xWriteCH392Cmd(CMD30_WRITE_SEND_BUF_SN);
    xWriteCH392Data((u8)sockindex);
    xWriteCH392Data((u8)len);
    xWriteCH392Data((u8)(len >> 8));
    for (i = 0; i < len; i++)
    {
        xWriteCH392Data(*databuf++);
    }
    xEndCH392Cmd();
}

/*******************************************************************************
 * Function Name  : CH392GetRecvLength
 * Description    : Gets the length of data received by socket n
 * Input          : sock index
 * Output         : None
 * Return         : 2 bytes receive length
 *******************************************************************************/
u16 CH392CMDGetRecvLength(u8 sockindex)
{
    u16 i;

    xWriteCH392Cmd(CMD12_GET_RECV_LEN_SN);
    xWriteCH392Data((u8)sockindex);
/*In between sending and receiving bytes, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    i = xReadCH392Data();
    i = (u16)(xReadCH392Data() << 8) + i;
    xEndCH392Cmd();
    return i;
}

/*******************************************************************************
 * Function Name  : CH392ClearRecvBuf
 * Description    : Clear receive buffer
 * Input          : sockindex
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDClearRecvBuf(u8 sockindex)
{
    xWriteCH392Cmd(CMD10_CLEAR_RECV_BUF_SN);
    xWriteCH392Data((u8)sockindex);
    xEndCH392Cmd();
}

/********************************************************************************
* Function Name  : CH392GetRecvLength
* Description    : Gets socket n receive buffer data
* Input          : sockindex
                   len
                   pbuf
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDGetRecvData(u8 sockindex, u16 len, u8 *pbuf)
{
    u16 i;
    if (!len)
        return;
    xWriteCH392Cmd(CMD30_READ_RECV_BUF_SN);
    xWriteCH392Data(sockindex);
    xWriteCH392Data((u8)len);
    xWriteCH392Data((u8)(len >> 8));
/*In between sending and receiving bytes, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    for (i = 0; i < len; i++)
    {
        *pbuf = xReadCH392Data();
        pbuf++;
    }
    xEndCH392Cmd();
}

/********************************************************************************
* Function Name  : CH392CMDGetSocketStatus
* Description    : Obtain the socket n status
* Input          : None
* Output         : First byte: socket n open or closed
                   Second byte: TCP state, meaningful only if TCP mode and the first byte is open
* Return         : None
*******************************************************************************/
u16 CH392CMDGetSocketStatus(u8 sockindex)
{
    u16 status;
    xWriteCH392Cmd(CMD12_GET_SOCKET_STATUS_SN);
    xWriteCH392Data(sockindex);
/*In between sending and receiving bytes, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    status = xReadCH392Data() << 8;
    status |= xReadCH392Data();
    xEndCH392Cmd();
    return status;
}

/*******************************************************************************
 * Function Name  : CH392OpenSocket
 * Description    : Open socket n
 * Input          : sockindex
 * Output         : None
 * Return         : s
 *******************************************************************************/
u8 CH392CMDOpenSocket(u8 sockindex)
{
    u8 i = 0;
    u8 s = 0;
    xWriteCH392Cmd(CMD1W_OPEN_SOCKET_SN);
    xWriteCH392Data(sockindex);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW;
    }
    return s;
}

/*******************************************************************************
 * Function Name  : CH392CloseSocket
 * Description    : Close socket n
 * Input          : sockindex
 * Output         : None
 * Return         : S
 *******************************************************************************/
u8 CH392CMDCloseSocket(u8 sockindex)
{
    u8 i = 0;
    u8 s = 0;
    xWriteCH392Cmd(CMD1W_CLOSE_SOCKET_SN);
    xWriteCH392Data(sockindex);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW;
    }
    return s;
}

/********************************************************************************
 * Function Name  : CH392TCPConnect
 * Description    : socket n connection. After receiving this command, socket n enters the client mode, valid only for TCP mode
 * Input          : sockindex
 * Output         : None
 * Return         : S
 *******************************************************************************/
u8 CH392CMDTCPConnect(u8 sockindex)
{
    u8 i = 0;
    u8 s = 0;
    xWriteCH392Cmd(CMD1W_TCP_CONNECT_SN);
    xWriteCH392Data(sockindex);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW;
    }
    return s;
}

/********************************************************************************
 * Function Name  : CH392CMDTCPDisconnect
 * Description    : socket n disconnection
 * Input          : sockindex
 * Output         : None
 * Return         : S
 *******************************************************************************/
u8 CH392CMDTCPDisconnect(u8 sockindex)
{
    u8 i = 0;
    u8 s = 0;
    xWriteCH392Cmd(CMD1W_TCP_DISNCONNECT_SN);
    xWriteCH392Data(sockindex);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW;
    }
    return s;
}

/******************************************************************************
 * Function Name  : CH392TCPListen
 * Description    : socket n listens, After receiving this command, socket n enters server mode, valid only for TCP mode
 * Input          : sockindex
 * Output         : None
 * Return         : S
 *******************************************************************************/
u8 CH392CMDTCPListen(u8 sockindex)
{
    u8 i = 0;
    u8 s = 0;
    xWriteCH392Cmd(CMD1W_TCP_LISTEN_SN);
    xWriteCH392Data(sockindex);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW;
    }
    return s;
}

/*******************************************************************************
 * Function Name  : CH392GetSocketInt
 * Description    : Gets the interrupt status of socket n
 * Input          : sockindex
 * Output         : None
 * Return         : socket interrupt status
 *******************************************************************************/
u8 CH392CMDGetSocketInt(u8 sockindex)
{
    u8 intstatus;
    xWriteCH392Cmd(CMD11_GET_INT_STATUS_SN);
    xWriteCH392Data(sockindex);
/*In between sending and receiving bytes, a TSC time delay is required.*/
#ifdef CH392T
    Delay_Us(1);
#endif
#ifdef CH392F
    Delay_Us(3);
#endif
    intstatus = xReadCH392Data();
    xEndCH392Cmd();
    return intstatus;
}

/******************************************************************************
 * Function Name  : CH392DHCPEnable
 * Description    : Enable DHCP
 * Input          : flag : 1 enable DHCP, 0 disable DHCP
 * Output         : None
 * Return         : s
 *******************************************************************************/
u8 CH392CMDDHCPEnable(u8 flag)
{
    u8 i = 0;
    u8 s;
    xWriteCH392Cmd(CMD10_DHCP_ENABLE);
    xWriteCH392Data(flag);
    xEndCH392Cmd();
    while (1)
    {
        Delay_Ms(20);               /* Delay query, more than 20MS is recommended*/
        s = CH392CMDGetCmdStatus(); /* Do not query too frequently*/
        if (s != CH392_ERR_BUSY)
            break;
        if (i++ > 200)
            return CH392_ERR_UNKNOW;
    }
    return s;
}

/******************************************************************************
 * Function Name  : CH392GetDHCPStatus
 * Description    : Obtain DHCP status
 * Input          : None
 * Output         : None
 * Return         : 1 byte status code, 0 indicates success, other values fail
 *******************************************************************************/
u8 CH392CMDGetDHCPStatus(void)
{
    u8 status;
    xWriteCH392Cmd(CMD01_GET_DHCP_STATUS);
    status = xReadCH392Data();
    xEndCH392Cmd();
    return status;
}

/*******************************************************************************
 * Function Name  : CH392GetIPInf
 * Description    : Get IP, subnet mask, gateway
 * Input          : None
 * Output         : 20 bytes, respectively 4 bytes IP, 4 bytes gateway, 4 bytes mask, 4 bytes DNS1, 4 bytes DNS2
 * Return         : None
 *******************************************************************************/
void CH392CMDGetIPInf(u8 *addr)
{
    u8 i;
    xWriteCH392Cmd(CMD014_GET_IP_INF);
    for (i = 0; i < 20; i++)
    {
        *addr++ = xReadCH392Data();
    }
    xEndCH392Cmd();
}

/*******************************************************************************
* Function Name  : CH392SetSocketRecvBuf
* Description    : Sets the SOCKET receive buffer
* Input          : sockindex
                 ��starting block index
                 ��the number of blocks
* Output         : None
* Return         : None
*******************************************************************************/
void CH392CMDSetSocketRecvBuf(u8 sockindex, u8 startblk, u8 blknum)
{
    xWriteCH392Cmd(CMD30_SET_RECV_BUF);
    xWriteCH392Data(sockindex);
    xWriteCH392Data(startblk);
    xWriteCH392Data(blknum);
}

/*******************************************************************************
 * Function Name  : CH392CMDGetGlobIntStatus_392T
 * Description    : Gets the CH392T global interrupt status
 * Input          : None
 * Output         : None
 * Return         : Global interrupt status
 *******************************************************************************/
u16 CH392CMDGetGlobIntStatus_392T(void)
{

    u16 init_status;
    xWriteCH392Cmd(CMD02_GET_GLOB_INT_STATUS_ALL);
    init_status = xReadCH392Data();
    init_status |= xReadCH392Data() << 8;
    xEndCH392Cmd();
    return init_status;
}

/*******************************************************************************
 * Function Name  : CH392CMDGetGlobIntStatus_392F
 * Description    : Gets the CH392F global interrupt status
 * Input          : None
 * Output         : None
 * Return         : Global interrupt status
 *******************************************************************************/
u8 CH392CMDGetGlobIntStatus_392F(void)
{

    u8 init_status;
    xWriteCH392Cmd(CMD01_GET_GLOB_INT_STATUS);
    init_status = xReadCH392Data();
    xEndCH392Cmd();
    return init_status;
}

/*******************************************************************************
 * Function Name  : CH392SetKeepLive
 * Description    : Set the socket n keeplive function
 * Input          : sockindex
 *                  cmd 0: close 1:open
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetKeepLive(u8 sockindex, u8 cmd)
{
    xWriteCH392Cmd(CMD20_SET_KEEP_LIVE_SN);
    xWriteCH392Data(sockindex);
    xWriteCH392Data(cmd);
}

/*******************************************************************************
 * Function Name  : CH392KeepLiveCNT
 * Description    : Set KEEPLIVE retries times
 * Input          : cnt : retry times
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDKeepLiveCNT(u8 cnt)
{
    xWriteCH392Cmd(CMD10_SET_KEEP_LIVE_CNT);
    xWriteCH392Data(cnt);
}

/*******************************************************************************
 * Function Name  : CH392KeepLiveIDLE
 * Description    : Set KEEPLIVE idle time
 * Input          : idle time (ms)
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDKeepLiveIDLE(UINT32 idle)
{
    xWriteCH392Cmd(CMD40_SET_KEEP_LIVE_IDLE);
    xWriteCH392Data((u8)idle);
    xWriteCH392Data((u8)((u16)idle >> 8));
    xWriteCH392Data((u8)(idle >> 16));
    xWriteCH392Data((u8)(idle >> 24));
}

/*******************************************************************************
 * Function Name  : CH392KeepLiveINTVL
 * Description    : Set KEEPLIVE interval time
 * Input          : timeout interval (ms)
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDKeepLiveINTVL(UINT32 intvl)
{
    xWriteCH392Cmd(CMD40_SET_KEEP_LIVE_INTVL);
    xWriteCH392Data((u8)intvl);
    xWriteCH392Data((u8)((u16)intvl >> 8));
    xWriteCH392Data((u8)(intvl >> 16));
    xWriteCH392Data((u8)(intvl >> 24));
}

/*******************************************************************************
 * Function Name  : CH392SetTTLNum
 * Description    : Set the TTL
 * Input          : sockindex
 *                  TTL
 * Output         : None
 * Return         : None
 *******************************************************************************/
void CH392CMDSetTTLNum(u8 sockindex, u8 TTLnum)
{
    xWriteCH392Cmd(CMD20_SET_TTL);
    xWriteCH392Data(sockindex);
    xWriteCH392Data(TTLnum);
}

/*******************************************************************************
 * Function Name  : CH392CMDGetSendQueue
 * Description    : Get 392 idle send queue
 * Input          : None
 * Output         : None
 * Return         : Number of 392 idle send queues
 *******************************************************************************/
u8 CH392CMDGetIdleSendQueue()
{
    u8 i;
    xWriteCH392Cmd(CMD01_GET_SEND_QUEUE);
    i = xReadCH392Data();
    xEndCH392Cmd();
    return i;
}

/*******************************************************************************
 * Function Name  : CH392SetUartBaudRate
 * Description    : Set 392 Uart BaudRate
 * Input          : BaudRate
 * Output         : None
 * Return         : s
 *******************************************************************************/
u8 CH392SetUartBaudRate(UINT32 baudrate)
{
    u8 s = CH392_ERR_UNKNOW;
#if (CH392_OP_INTERFACE_MODE == CH392_UART_MODE)
    CH392CMDSetUartBaudRate(baudrate); /* Set BaudRate */
    Delay_Ms(10);
    Set_MCU_BaudRate(baudrate);
    s = xReadCH392Data(); /* If the setting is successful��CH392 return CMD_ERR_SUCCESS */
    if (s == CMD_ERR_SUCCESS)
        printf("Set Success\r\n");
#endif
    return s;
}
